package com.siwei.mes.service.sync.tc.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.sync.tc.Waybill;
import com.siwei.mes.entity.sync.tc.WaybillEntity;
import com.siwei.mes.entity.vo.ExperimentVo;
import com.siwei.mes.entity.vo.WaybillVo;
import com.siwei.mes.enums.TcContrastExperimentTypeEnum;
import com.siwei.mes.enums.WaybillStateEnum;
import com.siwei.mes.mapper.experiment.ExperimentWaybillMapper;
import com.siwei.mes.mapper.syncTc.WaybillMapper;
import com.siwei.mes.service.sync.tc.WaybillService;
import com.siwei.mes.sync.tc.entity.GenMaterialResult;
import com.siwei.mes.sync.tc.service.GenMaterialBatchExperimentService;
import com.siwei.mes.sync.tc.service.GenMaterialQuickExperimentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 砼材-运单(Waybill)表服务实现类
 *
 * @author linzi
 * @since 2024-05-22 21:08:28
 */
@Service
public class WaybillServiceImpl implements WaybillService {
    @Resource
    private WaybillMapper waybillMapper;
    @Resource
    private ExperimentWaybillMapper experimentWaybillMapper;
    @Resource
    private GenMaterialQuickExperimentService genMaterialQuickExperimentService;
    @Resource
    private GenMaterialBatchExperimentService genMaterialBatchExperimentService;

    @Override
    public Long create(Waybill entity) {
        waybillMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Waybill entity) {
        waybillMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        waybillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Waybill> getPage(PageQuery<Waybill, Waybill> query) {
        Waybill searchParam = query.getParams();
        PageInfo<Waybill> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> waybillMapper.getList(searchParam));
        for (Waybill waybill : pageInfo.getList()) {
            if (ObjectUtil.isNotEmpty(waybill.getState())) {
                waybill.setStateName(WaybillStateEnum.selectByCode(waybill.getState()).getName());
            }
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Waybill> bindPage(PageQuery<Waybill, Waybill> query) {
        Waybill searchParam = query.getParams();
        /* if(searchParam.getCheckType()!=null){
           if(searchParam.getCheckType()==1){
               searchParam.setIsQuick(0);
           }else{
               searchParam.setIsBatch(0);
           }
        }*/
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> waybillMapper.bindList(searchParam));
    }

    @Override
    public Waybill loadById(Long id) {
        return waybillMapper.loadById(id);
    }

    @Override
    public List<Waybill> getAll(Waybill entity) {
        return waybillMapper.getList(entity);
    }

    @Override
    public List<Waybill> selectByExperimentId(Long experimentId, Long supplyTaskId) {
        return experimentWaybillMapper.selectByExperimentIdAndTask(experimentId, supplyTaskId);
    }

    @Override
    public PageInfo<Waybill> selectByExperimentIdPage(PageQuery<WaybillVo, WaybillVo> query) {
        WaybillVo searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentWaybillMapper.selectByExperimentIdAndTask(searchParam.getExperimentId(), searchParam.getSupplyTaskId()));
    }

    @Override
    public void batchGenerateExperiment(WaybillEntity waybillEntity) {
        assertTrue(CollectionUtil.isEmpty(waybillEntity.getWaybillIds()), "未传运单");
        for (Long id : waybillEntity.getWaybillIds()) {
            Waybill entity = waybillMapper.loadById(id);
            assertTrue(entity == null, "运单信息不存在");
            /**根据砼材的类型 对照出 MES这边的类型*/
            TcContrastExperimentTypeEnum typeEnum = TcContrastExperimentTypeEnum.selectByTcType(entity.getProjectCategory());
            assertTrue(typeEnum == null, "对照出类型为空，不生成委托，砼材对应类型：【{" + entity.getProjectCategory() + "}】");
            String msg = null;
            if (WaybillStateEnum.WAIT_WEIGH.getCode().equals(entity.getState())) {
                GenMaterialResult genMaterialResult = genMaterialQuickExperimentService.genExperiment(typeEnum, entity);
                if (genMaterialResult != null) {
                    msg = genMaterialResult.getErrorMsg();
                }
            } else if (WaybillStateEnum.DONE.getCode().equals(entity.getState())) {
                GenMaterialResult genMaterialResult = genMaterialBatchExperimentService.genExperiment(typeEnum, entity);
                if (genMaterialResult != null) {
                    msg = genMaterialResult.getErrorMsg();
                }
            } else {
                msg = "运单状态不为待过磅或已完成";
            }
            if (StringUtils.isNotBlank(msg)) {
                entity.setExperimentAbnormalInfo(msg);
            }
            waybillMapper.updateByPrimaryKey(entity);
        }
    }
}
